package com.joinway.framework.core.stub

import groovy.xml.StreamingMarkupBuilder

import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes


class XmlTest {

	static String curDir = ".."
//	static String workDir = "./groovydoc"
	static String workDir = "C:/"
	
	static main(args) {
//		test1()
		makepom()
//		test2()
	}

	static void test1(){
		Node project = new XmlParser().parse("../pom.xml")
		project.properties.each{
			println it
		}
		
//		Iterator it = project.properties.iterator()
//		println it.next()
//		println it.next()
		
		println project.properties[0].children()
		project.properties[0].children().each{
//			println it.getClass()
			println it.name().getLocalPart()
//			println it.value()
			println it.text()
		}
//		project.properties.childern().each{
//			println it.name()
//		}
	}
	
	static void test2(){
		Node project = new XmlParser().parse("../pom.xml")
		project.dependencies[0].dependency.each {
			println it.groupId[0].text()
			println it.artifactId[0].text()
			println it.version[0].text()
//			println it.scope?.get(0)?.text()
		}
	}
	
	static void makepom(){
		def root = new XmlParser().parse("${curDir}/pom.xml")
		Map versions = new HashMap()
		root.properties[0].children().each{
			versions.put(it.name().getLocalPart(), it.text())
		}
		
		String v = root.version.text()
		
//		println versions
			
		List<File> poms = []
		Path path = Paths.get(curDir)
		Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
			@Override
			public FileVisitResult visitFile(Path p, BasicFileAttributes attrs) throws IOException {
				if("pom.xml".equals(p.toFile().getName())){
					poms << p.toString()
				}
				return FileVisitResult.CONTINUE;
			}
		})
		
//		println poms

		List depInfoList = []
		List jars = []
		poms.each {
			root = new XmlParser().parse(it)
			if(root.dependencies && root.dependencies[0].dependency){
				root.dependencies[0].dependency.each {
					String groupId = it.groupId[0].text()
					String artifactId = it.artifactId[0].text()
					
					if(!jars.contains(groupId + "." + artifactId)){
						jars << groupId + "." + artifactId
						
						def depInfo = new Object()
						depInfo.metaClass.groupId = groupId
						depInfo.metaClass.artifactId = artifactId
						depInfo.metaClass.version = it.version[0].text()
						
						depInfoList << depInfo
					}
				}
			}
		}
		
		StreamingMarkupBuilder builder = new StreamingMarkupBuilder()
//		builder.encoding = "UTF-8"
		
		String p = "project.build.sourceEncoding"
		
		def project = {
//			mkp.xmlDeclaration()
			mkp.declareNamespace('':"http://maven.apache.org/POM/4.0.0")
			mkp.declareNamespace('xsi':"http://www.w3.org/2001/XMLSchema-instance")
			mkp.declareNamespace("schemaLocation':'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd")
			
			project{
				modelVersion("4.0.0")
				groupId("com.joinway")
				artifactId("framework")
				version("$v")
				
				properties{
					"$p"("UTF-8")
					versions.each {key, value ->
						"$key"("$value")
					}
				}
				
				build{
					plugins{
						plugin{
							groupId("org.codehaus.gmavenplus")
							artifactId("gmavenplus-plugin")
							version("1.4")
							executions{
								execution{
									goals{
										goal("addSources")
										goal("addTestSources")
										goal("generateStubs")
										goal("compile")
										goal("testCompile")
									}
								}
							}
							configuration{
								sources{
									source{
										directory("\${project.basedir}/src/main/java")
										includes{
											include("**/*.groovy")
										}
									}
								}
							}
						}
						plugin{
							groupId("org.apache.maven.plugins")
							artifactId("maven-compiler-plugin")
							version("2.3.2")
							configuration{
								source("1.8")
								target("1.8")
							}
						}
					}
				}
				
				dependencies{
					for(int i = 0; i < depInfoList.size(); i++){
						def node = depInfoList[i]
						dependency{
							groupId("$node.groupId")
							artifactId("$node.artifactId")
							version("$node.version")
						}
					}
					
					dependency{
						groupId("org.fusesource.jansi")
						artifactId("jansi")
						version("1.11")
					}
				}
			}
		}
		
//		println project
		
		FileWriter writer = new FileWriter("${workDir}/pom.xml")
		writer << builder.bind(project)
		
		println "generated ${workDir}/pom.xml"
	}
}
